package untiring07_HelloJDBC;

import java.util.Scanner;

//성적데이터를 Scanner 클래스로 입력받고 총점, 평균,학점을 계산한 후 그결과를
// SungJukVO에 저장하고, SungJukDAO로 그것을 넘김
public class EX01_SungJukService implements EX01_ISungJukService {
	private EX01_ISungJukDAO sjdao = null;
	
	public EX01_SungJukService() {
		sjdao = new EX01_SungJukDAO();
	}


	Scanner sc = new Scanner(System.in);
	EX01_SungJukVO sj = new EX01_SungJukVO();

	@Override
	public void createSungJuk() {
		System.out.println("성적을 입력해주세요");
		System.out.println("이름을 입력해주세요");
		sj.setName(sc.next());
		System.out.println("영어성적을 입력해주세요");
		sj.setEng(sc.nextInt());
		System.out.println("국어성적을 입력해주세요");
		sj.setKor(sc.nextInt());
		System.out.println("수학성적을 입력해주세요");
		sj.setMat(sc.nextInt());
		

		sj.setTot(this.totalSungJuk(sj.getEng(), sj.getKor(), sj.getMat()));
		sj.setAvg(this.avgSungJuk(sj.getTot()));
		sj.setGra(this.grdSungJuk(sj.getAvg()));

		if (sjdao.insertSungJuk(sj))
			System.out.println("성공");
		else
			System.out.println("실패");
	}

	
	@Override
	public void readSungJuk() {
		for(EX01_SungJukVO sj:sjdao.selectSungJuk()){

		System.out.print(sj.getSjno());
		System.out.print("/");
		
		System.out.print(sj.getName());
		System.out.print("/");
		System.out.print(sj.getEng());
		System.out.print("/");
		System.out.print(sj.getKor());
		System.out.print("/");
		System.out.print(sj.getMat());
		System.out.print("/");
		System.out.print(sj.getTot());
		System.out.print("/");
		System.out.print(sj.getAvg());
		System.out.print("/");
		System.out.println(sj.getGra());
		
		}
	}
	@Override
	public float avgSungJuk(int tot) {
		return tot / 3;
	}
	@Override
	public int totalSungJuk(int eng, int kor, int mat) {
		return kor + eng + mat;
	}
	public String grdSungJuk(float avg) {
		// String result = "";
		switch ((int) avg / 10) {
		case 10:
		case 9:
			// result = "수";
			return "수";
		case 8:
			//result = "우";
			return "우";
		case 7:
			//result = "미";
			return "미";
		case 6:
			//result = "양";
			return "양";
		default:
			//result = "가";
			return "가";
		}
	}


}
